<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Cookie;
use App\Http\Controllers\API\NBIotController;

class InfoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        // 获取权限
        $c_agent_id = $request->Cookie('logon')['agent_id'];
        $role_id = $request->Cookie('logon')['role_id'];
        // 获取代理商名称及ID
        $lts_name =  DB::table('agent')
            -> select('id','name')
            -> where(DB::raw("concat(',', path,',')"), 'like', '%,'.$c_agent_id.',%');
        $agname = DB::table('agent')
            -> select('id','name')
            -> where('id', $c_agent_id)
            -> union($lts_name)
            -> get();

        /*获取请求数据*/
        // 代理商
        $get['agent_id'] = $request->input('agent_id','');
        $agent_id = empty($get['agent_id']) ? $c_agent_id : $get['agent_id'];
        unset($get['agent_id']);
        $get['genre'] = $request->input('genre','');
        if ($get['genre']) {
            $genre = ' -> where("genre",$get["genre"])';
        } else {
            $genre = '';
        }
        $get['num'] = $request->input('num','');
        if ($get['num']) {
            $num = ' -> where("num",$get["num"])';
        } else {
            $num = '';
        }
        $get['DevEUI'] = $request->input('DevEUI','');
        if ($get['DevEUI']) {
            $eui = ' -> where("DevEUI","like","%".$get["DevEUI"])';
        } else {
            $eui = '';
        }
        // 时间区间
        $get['ftime'] = strtotime($request->input('ftime',date('Y-m-d H:i:s',time()-3600*720*3)));
        $get['ltime'] = strtotime($request->input('ltime',''));
        if($get['ltime']){
            $get['ltime'] = $get['ltime']+(3600*24)-1;
        }else{
            $get['ltime'] = time();
        }
        // 管理范围
        $area_ids = DB::table('role')->where('id',$role_id)->value('area_ids');
        if($area_ids==='0'){
            $aname = '';
        }else{
            $rarea = explode(',',$area_ids);
            foreach ($rarea as $k=>$v) {
                $rarea[$k] = DB::table('area')->where('id',$v)->value('name');
            }
            $aname = ' ->whereIn("aname",$rarea)';
        }
        $sqlstr = 'DB::table("alllog") -> where("agent_id",$agent_id)'.$genre.$num.$eui.$aname.' -> whereBetween("time",[$get["ftime"],$get["ltime"]]) ->orderBy("time", "desc") -> paginate(10);';
        $data = '';
        eval('$data ='.$sqlstr);
        return view('admin.info.index',['data'=>$data,'areaids'=>$area_ids,'get'=>$get,'agname'=>$agname,'request'=>$request->all()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getShow()
    {
        $nbiot = new NBIotController();
        // dd($nbiot->add_devices('868613030001284'));
        // dd($nbiot->update_devices('e74b5286-4e2c-48ae-a28b-cf29a0243447','868613030001284'));
        dd($nbiot->delete_devices('28998e2d-5197-4e65-b66a-1ee72600e841'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
